/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.bussines.ejbs;

import cl.bussines.entities.Permisos;
import cl.bussines.entities.Rol;
import cl.bussines.entities.RolHasPermisos;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Vogel
 */
@Stateless
public class PermisosFacade extends AbstractFacade<Permisos> {
    @PersistenceContext(unitName = "TallerStudioPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PermisosFacade() {
        super(Permisos.class);
    }
    
    public List<Permisos> getPermisosByRol(Rol rol){
        List<Permisos> lista = new ArrayList<Permisos>();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("TallerStudioPU");
        EntityManager em2 = emf.createEntityManager();
        String sql1 = "select r  from RolHasPermisos r where r.idRol = :rol";
        TypedQuery<RolHasPermisos> query1 = em2.createQuery(sql1, RolHasPermisos.class);
        query1.setParameter("rol", rol);
        List<RolHasPermisos> relacion = query1.getResultList();
        
        for(RolHasPermisos per : relacion){
            String sql = "SELECT p FROM Permisos p WHERE p.idPermiso = :permiso";
            TypedQuery<Permisos> query = em2.createQuery(sql, Permisos.class);
            query.setParameter("permiso", per.getIdPermiso().getIdPermiso());
            lista.add(query.getSingleResult());
        }
        return lista;
    }
    
}
